package com.sihai.ehr.service.impl;

import java.util.List;

import com.sihai.core.*;
import com.sihai.ehr.model.biz.Duty;
import com.sihai.ehr.model.biz.Grade;
import com.sihai.ehr.service.DutyService;
import com.sihai.ehr.service.GradeService;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;

/**
 * User: xiao Date: 12-6-7
 */
@Service("gradeService")
public class GradeServiceImpl implements GradeService {
	@Resource
	private DataAccessor dataAccessor;

	@Override
	public List<Grade> search(final Long dutyId,
			final String groupByColumnName, Limit limit, Sorter sorter) {

		if (dutyId < 0) {
			return null;
		}
		try {
			return dataAccessor.findByQuery(Grade.class, new QueryCreator() {
				@Override
				public Query createQuery(Session session) {
					DynamicQuery dynamicQuery = new DynamicQuery(
							"select new Grade(max(g.id),g.gradeName) from Grade g where g.cancel <> 1 ");
					if (dutyId > 0) {
						dynamicQuery.appendIfNotNull("and g.duty.id = :dutyId",
								"dutyId", dutyId);
					}
					if (groupByColumnName != null
							&& !groupByColumnName.isEmpty()) {
						dynamicQuery.append(" group by g." + groupByColumnName);
					}
					return dynamicQuery.build(session);
				}
			}, limit, sorter);
		} catch (Exception e) {
			throw new RuntimeException("【异常原因：获取职级数据库发生异常】【异常说明：+"
					+ e.getMessage()
					+ "】【发生地点：GradeServiceImpl.search;参数dutyId=" + dutyId
					+ " 】");
		}
	}

	@Override
	public List<Grade> searchByGradeName(final Long dutyId,
			final String gradeName, Limit limit, Sorter sorter) {

		if (dutyId < 0) {
			return null;
		}
		try {
			return dataAccessor.findByQuery(Grade.class, new QueryCreator() {
				@Override
				public Query createQuery(Session session) {
					DynamicQuery dynamicQuery = new DynamicQuery(
							"select new Grade(g.id,g.gradeName,g.gradeSort) from Grade g where g.cancel <> 1 ");
					if (dutyId > 0) {
						dynamicQuery.appendIfNotNull("and g.duty.id = :dutyId",
								"dutyId", dutyId);
					}
					if (gradeName != null && !gradeName.isEmpty()) {
						dynamicQuery.appendIfNotNull(
								"and g.gradeName = :gradeName", "gradeName",
								gradeName);
					}
					return dynamicQuery.build(session);
				}
			}, limit, sorter);
		} catch (Exception e) {
			throw new RuntimeException("【异常原因：获取职级数据库发生异常】【异常说明：+"
					+ e.getMessage()
					+ "】【发生地点：GradeServiceImpl.searchByGradeName;参数dutyId="
					+ dutyId + ",gradeName=" + gradeName + "】");
		}
	}

	@Override
	public LimitedList<Grade> search(final Grade grade, Limit limit,
			Sorter sorter) {
		if (grade == null) {
			return dataAccessor.findByClass(Grade.class, limit, sorter);
		}

		return dataAccessor.findByQuery(Grade.class, new QueryCreator() {
			@Override
			public Query createQuery(Session session) {
				DynamicQuery dynamicQuery = new DynamicQuery(
						"from Grade g where g.cancel <> 1 ");
				if(grade != null){
					if (grade.getDuty() != null) {
						dynamicQuery.appendIfNotNull("and g.duty.id = :duty",
								"duty", grade.getDuty().getId());
					}
					dynamicQuery.appendIfNotNull("and g.gradeName like :gradeName",
							"gradeName", grade.getGradeName());
					dynamicQuery.appendIfNotNull("and g.gradeSort like :gradeSort",
							"gradeSort", grade.getGradeSort());
					dynamicQuery.appendIfNotNull(
							"and g.dutyContent like :gradeContent", "gradeContent",
							grade.getGradeContent());
				}
				return dynamicQuery.build(session);
			}
		}, limit, sorter);
	}

	@Override
	public void save(Grade grade) {
		if (grade != null) {
			dataAccessor.save(grade);
		}
	}

	@Override
	public void remove(final String gradeIds) {
		if (gradeIds == null || gradeIds.isEmpty()) {
			return;
		}

		dataAccessor.batchUpdate(new QueryCreator() {
			@Override
			public Query createQuery(Session session) {
				return session.createQuery(String.format(
						"update Grade g set g.cancel = 1 where g.id in (%s)",
						gradeIds));
			}
		});
	}

	@Override
	public Grade get(Long id) {
		if (id == null) {
			return null;
		}

		return dataAccessor.get(Grade.class, id);
	}

    @Override
    public List<Grade> findGradeByDutyId(Long dutyId, Limit limit, Sorter sorter) {
        Assert.notNull(dutyId, "duty id is required");
        return dataAccessor.findByHql(Grade.class, "select new Grade(g.id, g.gradeName) from Grade g where g.duty.id = ? group by g.gradeName", new Object[]{dutyId}, limit, sorter);
    }

    @Override
    public List<Grade> findGradeLevelByGrade(Grade grade, Limit limit, Sorter sorter) {
        Assert.notNull(grade, "grade id is required");
        return dataAccessor.findByHql(Grade.class, "select new Grade(g.id, g.gradeName, g.gradeSort) from Grade g where g.duty.id=? and g.gradeName=?", new Object[]{grade.getDuty().getId(), grade.getGradeName()}, limit, sorter);
    }

    @Override
    public List<Grade> findGradeAndLevelByDuty(Long dutyId, Limit limit, Sorter sorter) {
        Assert.notNull(dutyId, "duty id is required");
        return dataAccessor.findByHql(Grade.class, "select new Grade(g.id, g.gradeName, g.gradeSort) from Grade g where g.duty.id=? order by g.gradeName", new Object[]{dutyId}, limit, sorter);
    }
}
